﻿using Alimentador.Foods;
using Alimentador.RightGroup.Results;
using System;
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Media;

namespace Alimentador.Results
{
    /// <summary>
    /// Interaction logic for ResultsCtrl.xaml
    /// </summary>
    public partial class ResultsCtrl : UserControl
    {
        /// <summary>
        /// Constructor
        /// </summary>
        public ResultsCtrl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Load elements according to property list
        /// </summary>
        public void LoadElements(int numDays = 1)
        {
            PropertyListView.Items.Clear();

            foreach (PropertyName prop in Enum.GetValues(typeof(PropertyName)))
            {
                if (prop == PropertyName.Ficha || prop == PropertyName.Percentagem || prop == PropertyName.Porcao)
                {
                    continue;
                }

                ResultItem res = new ResultItem();
                res.Width = 350;
                res.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
                res.SetNameLblContent = MainHelper.GetEnumDescription(prop);
                res.Tag = prop;

                if (MainWindow.CurrentPatient != null)
                {
                    switch (prop)
                    {
                        case PropertyName.Energia:
                            {
                                res.RecTxtBoxText = FoodFormulas.RecommendedEnergy(numDays).ToString("F0");
                                break;
                            }
                        case PropertyName.Agua:
                            {
                                res.RecTxtBoxText = FoodFormulas.RecommendedWater(numDays).ToString("F0");
                                break;
                            }
                        case PropertyName.Proteina:
                            {
                                res.RecTxtBoxText = FoodFormulas.RecommendedProtein(numDays).ToString("F2");
                                break;
                            }
                        case PropertyName.GorduraTotal:
                            {
                                res.RecTxtBoxText = FoodFormulas.RecommendedLipids(numDays).ToString("F2");
                                break;
                            }
                        case PropertyName.TotalHidratosCarbono:
                            {
                                res.RecTxtBoxText = FoodFormulas.RecommendedCarbonHidrats(numDays).ToString("F2");
                                break;
                            }
                        case PropertyName.TotalHidratosCarbonoMono:
                            {
                                res.ShowHideRecomended = false;
                                break;
                            }
                        case PropertyName.MonoDissacaridos:
                            {
                                res.RecTxtBoxText = FoodFormulas.RecommendedMonoDissacaridos(numDays, MainWindow.CurrentPatient.PatGender).ToString("F2");
                                break;
                            }
                        case PropertyName.AcidosOrganicos:
                        case PropertyName.Alcool:
                        case PropertyName.Amido:
                        case PropertyName.Oligassacaridos:
                            {
                                res.ShowHideRecomended = false;
                                break;
                            }
                        case PropertyName.FibraAlimentar:
                            {
                                res.RecTxtBoxText = FoodFormulas.RecommendedFiber(numDays).ToString("F2");
                                break;
                            }
                        case PropertyName.AcidosGordosSaturados:
                            {
                                res.RecTxtBoxText = FoodFormulas.RecommendedAcidosGordosSaturados(numDays, MainWindow.CurrentPatient.PatGender).ToString("F2");
                                break;
                            }
                        case PropertyName.AcidosGordosMonoinsaturados:
                            {
                                res.RecTxtBoxText = FoodFormulas.RecommendedAcidosGordosMonoinsaturados(numDays, MainWindow.CurrentPatient.PatGender).ToString("F2");
                                break;
                            }
                        case PropertyName.AcidosGordosPolinsaturados:
                            {
                                res.RecTxtBoxText = FoodFormulas.RecommendedAcidosGordosPolinsaturados(numDays, MainWindow.CurrentPatient.PatGender).ToString("F2");
                                break;
                            }
                        case PropertyName.AcidosGordosTrans:
                        case PropertyName.AcidosLinoleico:
                            {
                                res.ShowHideRecomended = false;
                                break;
                            }
                        case PropertyName.Colesterol:
                            {
                                res.RecTxtBoxText = FoodFormulas.RecommendedColesterol(numDays).ToString("F2");
                                break;
                            }
                        case PropertyName.VitaminaATotal:
                            {
                                res.RecTxtBoxText = (FoodFormulas.RecommendedVitaminA(MainWindow.CurrentPatient) * numDays).ToString("F2");
                                break;
                            }
                        case PropertyName.Caroteno:
                            {
                                res.ShowHideRecomended = false;
                                break;
                            }
                        case PropertyName.VitaminaD:
                            {
                                res.RecTxtBoxText = (FoodFormulas.RecommendedVitaminD(MainWindow.CurrentPatient) * numDays).ToString("F2");
                                break;
                            }
                        case PropertyName.ATocoferol:
                            {
                                res.RecTxtBoxText = (FoodFormulas.RecommendedATocoferol(MainWindow.CurrentPatient) * numDays).ToString("F2");
                                break;
                            }
                        case PropertyName.Tiamina:
                            {
                                res.RecTxtBoxText = (FoodFormulas.RecommendedTiamina(MainWindow.CurrentPatient) * numDays).ToString("F2");
                                break;
                            }
                        case PropertyName.Riboflavina:
                            {
                                res.RecTxtBoxText = (FoodFormulas.RecommendedRiboflavina(MainWindow.CurrentPatient) * numDays).ToString("F2");
                                break;
                            }
                        case PropertyName.EquivalentesDeNiacina:
                            {
                                res.ShowHideRecomended = false;
                                break;
                            }
                        case PropertyName.Niacina:
                            {
                                res.RecTxtBoxText = (FoodFormulas.RecommendedNiacina(MainWindow.CurrentPatient) * numDays).ToString("F2");
                                break;
                            }
                        case PropertyName.Triptofano60:
                            {
                                res.ShowHideRecomended = false;
                                break;
                            }
                        case PropertyName.VitaminaB6:
                            {
                                res.RecTxtBoxText = (FoodFormulas.RecommendedVitaminB6(MainWindow.CurrentPatient) * numDays).ToString("F2");
                                break;
                            }
                        case PropertyName.VitaminaB12:
                            {
                                res.RecTxtBoxText = (FoodFormulas.RecommendedVitaminB12(MainWindow.CurrentPatient) * numDays).ToString("F2");
                                break;
                            }
                        case PropertyName.VitaminaC:
                            {
                                res.RecTxtBoxText = (FoodFormulas.RecommendedVitaminC(MainWindow.CurrentPatient) * numDays).ToString("F2");
                                break;
                            }
                        case PropertyName.Folatos:
                            {
                                res.RecTxtBoxText = (FoodFormulas.RecommendedFolatos(MainWindow.CurrentPatient) * numDays).ToString("F2");
                                break;
                            }
                        case PropertyName.Cinza:
                            {
                                res.ShowHideRecomended = false;
                                break;
                            }
                        case PropertyName.Sodio:
                            {
                                res.RecTxtBoxText = (FoodFormulas.RecommendedMineralSodium(MainWindow.CurrentPatient) * numDays).ToString("F2");
                                break;
                            }
                        case PropertyName.Potassio:
                            {
                                res.RecTxtBoxText = (FoodFormulas.RecommendedMineralPotassium(MainWindow.CurrentPatient) * numDays).ToString("F2");
                                break;
                            }
                        case PropertyName.Calcio:
                            {
                                res.RecTxtBoxText = (FoodFormulas.RecommendedMineralCalcium(MainWindow.CurrentPatient) * numDays).ToString("F2");
                                break;
                            }
                        case PropertyName.Fosforo:
                            {
                                res.RecTxtBoxText = (FoodFormulas.RecommendedMineralPhosphor * numDays).ToString("F2");
                                break;
                            }
                        case PropertyName.Magnesio:
                            {
                                res.RecTxtBoxText = (FoodFormulas.RecommendedMineralMagnesium(MainWindow.CurrentPatient) * numDays).ToString("F2");
                                break;
                            }
                        case PropertyName.Ferro:
                            {
                                res.RecTxtBoxText = (FoodFormulas.RecommendedMineralIron(MainWindow.CurrentPatient) * numDays).ToString("F2");
                                break;
                            }
                        case PropertyName.Zinco:
                            {
                                res.RecTxtBoxText = (FoodFormulas.RecommendedMineralZinc(MainWindow.CurrentPatient) * numDays).ToString("F2");
                                break;
                            }
                    }
                }

                res.UnitLblContent = FoodProperty.SetUnitByPropertyName(prop);
                PropertyListView.Items.Add(res);
            }
        }

        /// <summary>
        /// Load obtained values
        /// </summary>
        /// <param name="foodList">List(food)</param>
        /// <param name="numDays">int</param>
        public void LoadObtainedValues(List<FoodBase> foodList, int numDays = 1)
        {
            foreach (ResultItem res in PropertyListView.Items)
            {
                PropertyName prop = (PropertyName)res.Tag;

                if (prop == PropertyName.Ficha || prop == PropertyName.Percentagem || prop == PropertyName.Porcao)
                {
                    continue;
                }

                if (prop == PropertyName.Energia)
                {
                    res.ObtainedTxtBoxText = FoodFormulas.ObtainedPropertyByFoodList(foodList, prop).ToString("F0");
                }
                else
                {
                    res.ObtainedTxtBoxText = FoodFormulas.ObtainedPropertyByFoodList(foodList, prop).ToString("F2");
                }

                double ob = 0;
                double rec = 0;

                Double.TryParse(res.ObtainedTxtBoxText, out ob);
                Double.TryParse(res.RecTxtBoxText, out rec);

                if (ob > rec)
                {
                    res.RecTxtBox.BorderBrush = System.Windows.Media.Brushes.Red;
                }
                else
                {
                    res.RecTxtBox.BorderBrush = (SolidColorBrush)new BrushConverter().ConvertFromString("#FFABADB3");
                }
            }
        }
    }
}
